<?php
class JasperReport {

/**
 * 
 * see if the java extension was loaded.
 */
	public $outputPath;
public function getReport($reportName,$params,$conn=null)
{
	
		
		error_reporting(E_ALL);
		
		//echo realpath("report/report1.jrxml");
		//exit();
		$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
		
		$report = $compileManager->compileReport(realpath("report/$reportName.jrxml"));
		//$report=realpath("report/".$reportName.".jasper");

		$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
		
		/* JasperReport jasperReport = JasperCompileManager.compileReport(path);
		JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, param, conn);
		JRDocxExporter exporter = new JRDocxExporter();
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		exporter.setParameter(JRDocxExporterParameter.OUTPUT_FILE_NAME, new File("test.docx"));
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os); */
		
		//exporter.exportReport();
		//$params = new Java("java.util.HashMap");

		
		//$params->put("text", "Text");
		//$params->put("number", 3.00);
		//$params->put("date", $report->convertValue("2007-12-31 0:0:0", "java.sql.Timestamp"));
		
		
		
		
		
		
		$jasperPrint = $fillManager->fillReport($report, $params, $conn);
		$filename = uniqid('Report_');
		$this->outputPath = realpath(".")."/report/"."{$filename}.docx";
		$exporter = new java("net.sf.jasperreports.engine.export.ooxml.JRDocxExporter");
		$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
		$exporter->setParameter(java("net.sf.jasperreports.engine.export.ooxml.JRDocxExporterParameter")->OUTPUT_FILE_NAME, $this->outputPath);
		$exportManager = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
		$exporter->exportReport();
		//$exportManager->exportReportToPdfFile($jasperPrint, $this->outputPath);
		return $this->outputPath;
}	
function checkJavaExtension()
{
        if(!extension_loaded('java'))
        {
                $sapi_type = php_sapi_name();
                $port = (isset($_SERVER['SERVER_PORT']) && (($_SERVER['SERVER_PORT'])>1024)) ? $_SERVER['SERVER_PORT'] : '8080';
                if ($sapi_type == "cgi" || $sapi_type == "cgi-fcgi" || $sapi_type == "cli") 
                {
                        if(!(PHP_SHLIB_SUFFIX=="so" && @dl('java.so'))&&!(PHP_SHLIB_SUFFIX=="dll" && @dl('php_java.dll'))&&!(@include_once("java/Java.inc"))&&!(require_once("http://127.0.0.1:$port/java/Java.inc"))) 
                                if(!(require_once("./java/Java.inc"))) 
                        {
                                return "java extension not installed.";
                        }
                } 
                else
                {
                        if(!(@include_once("java/Java.inc")))
                        {
                                require_once("http://127.0.0.1:$port/javabridge/java/Java.inc");
                        }
                }
        }
        if(!function_exists("java_get_server_name")) 
        {
                return "The loaded java extension is not the PHP/Java Bridge";
        }

        return true;
}

/** 
 * convert a php value to a java one... 
 * @param string $value 
 * @param string $className 
 * @returns boolean success 
 */  
function convertValue($value, $className)  
{  
        // if we are a string, just use the normal conversion  
        // methods from the java extension...  
        try   
        {  
                if ($className == 'java.lang.String')  
                {  
                        $temp = new Java('java.lang.String', $value);  
                        return $temp;  
                }  
                else if ($className == 'java.lang.Boolean' ||  
                        $className == 'java.lang.Integer' ||  
                        $className == 'java.lang.Long' ||  
                        $className == 'java.lang.Short' ||  
                        $className == 'java.lang.Double' ||  
                        $className == 'java.math.BigDecimal')  
                {  
                        $temp = new Java($className, $value);  
                        return $temp;  
                }  
                else if ($className == 'java.sql.Timestamp' ||  
                        $className == 'java.sql.Time')  
                {  
                        $temp = new Java($className);  
                        $javaObject = $temp->valueOf($value);  
                        return $javaObject;  
                }  
        }  
        catch (Exception $err)  
        {  
                echo (  'unable to convert value, ' . $value .  
                                ' could not be converted to ' . $className);  
                return false;  
        }
  
        echo (  'unable to convert value, class name '.$className.  
                        ' not recognised');  
        return false;  
}
	
	
}
?>